package wyh.controller.Appointment;

import com.fasterxml.jackson.databind.ObjectMapper;
import wyh.pojo.Appointment2;
import wyh.pojo.User;
import wyh.service.AppointmentService;
import wyh.service.UserService;
import wyh.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//9666666666666666666 ok
@WebServlet("/Appointment/getAppointmentByUserId2")
public class getAppointmentByUserId2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int UserId = Integer.parseInt(req.getParameter("UserId"));
        UserService service0 = new UserService();
        User pojo = service0.ServicegetById(UserId);

        AppointmentService service = new AppointmentService();
        List<Appointment2> userList = service.ServicegetAll2();

        System.out.println(UserId+" (\"/Appointment/getAppointmentByUserId2\")" + pojo.getPatient_id());

        List<Appointment2> userList2 = new ArrayList<>();
        for (Appointment2 user : userList) {
            if (Objects.equals(user.getEscortId(), pojo.getEscort_id())) {
                userList2.add(user);
            }
        }

        Result result= new Result(200,"查询成功",userList2);
        ObjectMapper ojmapper = new ObjectMapper();
        String json = ojmapper.writeValueAsString(result);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }
}
